package com.sansan.qiangji.realize.wage;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import com.sansan.qiangji.VO.wage.PickingStockWageListResultVO;
import com.sansan.qiangji.VO.wage.PickingStockWageVO;
import com.sansan.qiangji.entity.wage.PickingStockWage;
import com.sansan.qiangji.entity.wage.example.PickingStockWageExample;
import com.sansan.qiangji.enums.ResultEnum;
import com.sansan.qiangji.exception.ServiceException;

import com.sansan.qiangji.form.wage.PickingStockWageAddForm;
import com.sansan.qiangji.form.wage.PickingStockWageEditForm;
import com.sansan.qiangji.form.wage.PickingStockWageListForm;
import com.sansan.qiangji.service.inter.wage.PickingStockWageService;
import com.sansan.qiangji.utils.CopyUtil;
import com.sansan.qiangji.utils.pageUtils.PageResult;
import com.sansan.qiangji.utils.pageUtils.PageUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
@Slf4j
public class PickingStockWageReal {
    @Autowired
    private PickingStockWageService pickingStockWageService;

    @Transactional
    public void deletePickingStockWage(Integer pickingStockWageId){
        PickingStockWage pickingStockWage = pickingStockWageService.selectByPrimaryKey(pickingStockWageId);
        if(pickingStockWage==null){
            log.error("【拣货单删除】拣货单不存在，pickingStockWageId={}",pickingStockWageId);
            throw new ServiceException(ResultEnum.DEVICE_IS_NOT_EXIST);
        }
        int row = pickingStockWageService.deleteByPrimaryKey(pickingStockWageId);
        if(row==0){
            log.error("【拣货单删除】拣货单删除失败，pickingStockWageId={}",pickingStockWageId);
            throw new ServiceException(ResultEnum.DEVICE_DELETE_ERROR);
        }
    }

    @Transactional
    public void addPickingStockWage(PickingStockWageAddForm pickingStockWageAddForm){
        PickingStockWageExample pickingStockWageExample = new PickingStockWageExample();
     
        PickingStockWage pickingStockWage = new PickingStockWage();
        BeanUtils.copyProperties(pickingStockWageAddForm,pickingStockWage);
        pickingStockWage.setCreateTime(new Date());
        pickingStockWage.setTotalPrice(pickingStockWageAddForm.getPrice()*pickingStockWageAddForm.getAmount());
        int row = pickingStockWageService.insert(pickingStockWage);
        if(row==0){
            log.error("【拣货单新增】拣货单新增失败");
            throw new ServiceException(ResultEnum.DEVICE_INSERT_ERROR);
        }
    }



    @Transactional
    public void editPickingStockWage(PickingStockWageEditForm pickingStockWageEditForm){
        PickingStockWage pickingStockWage = pickingStockWageService.selectByPrimaryKey(pickingStockWageEditForm.getId());
        if (pickingStockWage == null){
            log.error("【编辑物品】物品不存在，pickingStockWageEditForm={}",pickingStockWageEditForm.toString());
            throw new ServiceException(ResultEnum.DEVICE_IS_NOT_EXIST);
        }
        CopyUtil.copyPropertiesNotNull(pickingStockWageEditForm,pickingStockWage);
        pickingStockWage.setTotalPrice(pickingStockWageEditForm.getPrice()*pickingStockWageEditForm.getAmount());

        int row = pickingStockWageService.updateByPrimaryKey(pickingStockWage);
        if(row==0){
            log.error("【物品新增】物品新增失败");
            throw new ServiceException(ResultEnum.DEVICE_INSERT_ERROR);
        }
    }


    public PickingStockWageListResultVO pickingStockWageList(PickingStockWageListForm pickingStockWageListForm){
        PickingStockWageExample pickingStockWageExample = new PickingStockWageExample();
        int pageNum = pickingStockWageListForm.getPageNum();
        int pageSize = pickingStockWageListForm.getPageSize();
        PageHelper.startPage(pageNum,pageSize);
        PickingStockWageExample.Criteria criteria = pickingStockWageExample.createCriteria();

        List<PickingStockWage> pickingStockWageList = pickingStockWageService.selectByExample(pickingStockWageExample);
        PageResult<PickingStockWage> pickingStockWagePageResult = PageUtil.getPageResult(new PageInfo<>(pickingStockWageList));
        PickingStockWageListResultVO pickingStockWageListResultVO = new PickingStockWageListResultVO();
        BeanUtils.copyProperties(pickingStockWagePageResult,pickingStockWageListResultVO);
        List<PickingStockWageVO> pickingStockWageVOList = new ArrayList<>();
        for(PickingStockWage pickingStockWage : pickingStockWagePageResult.getContent()){
            PickingStockWageVO pickingStockWageVO = new PickingStockWageVO();
            BeanUtils.copyProperties(pickingStockWage,pickingStockWageVO);
            pickingStockWageVOList.add(pickingStockWageVO);
        }
        pickingStockWageListResultVO.setPickingStockWageVOS(pickingStockWageVOList);
        return pickingStockWageListResultVO;
    }
}
